<template>
  <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
      <el-form-item label="订单编号" prop="id">
          <el-input
            v-model="queryParams.id"
            placeholder="请输入订单编号"
            clearable
            class="!w-240px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="龙亿编号" prop="code">
          <el-input
            v-model="queryParams.code"
            placeholder="请输入龙亿编号"
            clearable
            class="!w-240px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="订单日期" prop="orderDateReq">
          <el-date-picker
            v-model="queryParams.orderDateReq"
            value-format="YYYY-MM-DD"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="订单状态"
              clearable
              class="!w-240px"
            >
              <el-option
                v-for="dict in dictType.list"
                :key="dict.id"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
          <el-button
                type="warning"
                plain
                @click="handleImport"
              >
                <Icon icon="ep:upload" /> 导入
              </el-button>
          <el-button
            type="success"
            plain
            @click="handleOutport"
            :loading="exportLoading"
            v-hasPermi="['system:post:export']"
          >
            <Icon icon="ep:download" class="mr-5px" /> 下载模板
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
  <!-- 列表 -->
  <ContentWrap>
      <el-table v-loading="loading" :data="list">
        <el-table-column type="index" label="序号" align="center" width="55" />
        <el-table-column label="订单编号" align="center" prop="id" />
        <el-table-column label="订单日期" align="center" prop="orderDateStr" />
        <el-table-column label="订单总数" align="center" prop="totalNum" />
        <el-table-column label="到货点" align="center" prop="arrivalPoint" />
        <el-table-column label="出货方式" align="center" prop="deliveryMode" />
        <el-table-column label="出货日期" align="center" prop="shippingDateStr" />
        <el-table-column label="客户" align="center" prop="customerName" />
        <el-table-column label="龙亿编号" align="center" prop="code" />
        <el-table-column label="订单状态" align="center" prop="status">
          <template #default="scope">
              <div class="flex items-center justify-center">{{ scope.row.status==='0' ? '未开始': scope.row.status==='1' ? '已完成': 
                scope.row.status==='2' ? '执行中': scope.row.status==='3' ? '部分结束': '' }}</div>
            </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" width="160">
            <template #default="scope">
              <div class="flex items-center justify-center">
                <el-button
                  type="primary"
                  link
                  @click="openDetail(scope.row.id)"
                >查看
                </el-button>
              </div>

              <div class="flex items-center justify-center">
                <el-button
                  link
                  type="danger"
                  v-hasPermi="['project:order:delete']"
                  @click="handleDelete(scope.row.id)"
                >
                  删除
                </el-button>
              </div>
            </template>
          </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
    <!-- 导入对话框 -->
    <orderImportForm ref="importFormRef" @success="getList" />
    <!-- 导出对话框 -->
    <orderOutportForm ref="outportFormRef" />
     
  </template>
  
  <script lang="ts" setup>
  import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
  import download from '@/utils/download'
  import * as orderApi from '@/api/hcxly/order/Index'
  import orderImportForm from './orderImportForm.vue'
  import orderOutportForm from './orderOutportForm.vue'
  import orderDetail from './orderDetail/orderDetail.vue'
  import {WorkstationApi} from "@/api/hcxly/workstation";

  
  defineOptions({ name: 'SystemPost' })
  
  const router = useRouter() // 路由
  const message = useMessage() // 消息弹窗
  const { t } = useI18n() // 国际化
  
  const loading = ref(true) // 列表的加载中
  const total = ref(0) // 列表的总页数
  const list = ref([]) // 列表的数据

  const queryParams = reactive({
    pageNo: 1,
    pageSize: 10,
    id: undefined,
    orderDateReq: [],
    operator: undefined,
    code: '',
    status : undefined
  })
  const queryFormRef = ref() // 搜索的表单
  const exportLoading = ref(false) // 导出的加载中
  
  const querytype = reactive({
    pageNo: 1,
    pageSize: 100,
    dictType: 'order_status'
  })
  const dictType = ref([])

  /** 查询状态 */
  const getDictType = async () => {
    dictType.value = await orderApi.getStatus(querytype)
    // console.log(dictType.value)
  }

  /** 查询列表 */
  const getList = async () => {
    loading.value = true
    try {
      const data = await orderApi.getOrderPage(queryParams)
      // console.log(data)
      list.value = data.list
      total.value = data.total
    } finally {
      loading.value = false
    }
  }
  
  /** 搜索按钮操作 */
  const handleQuery = () => {
    queryParams.pageNo = 1
    getList()
  }
  
  /** 重置按钮操作 */
  const resetQuery = () => {
    queryFormRef.value.resetFields()
    handleQuery()
  }
  
  /** 导入 */
  const importFormRef = ref()
  const handleImport = () => {
    importFormRef.value.open()
  }

  /** 导出 */
  const outportFormRef = ref()
  const handleOutport = () => {
    outportFormRef.value.open()
  }

  /** 查看详情 */
  const { push } = useRouter()
  const openDetail = (id) => {
    router.push({
      name: 'orderDetail',
      query: {
        id:id
      }
    })
  }

  /** 删除按钮操作 */
  const handleDelete = async (id: number) => {
    try {
      // 删除的二次确认
      await message.delConfirm()
      // 发起删除
      await orderApi.deleteOrder(id)
      message.success(t('common.delSuccess'))
      // 刷新列表
      await getList()
    } catch {}
  }

  /** 初始化 **/
  onMounted(() => {
    getDictType()
    getList()
  })
  
  </script>
